package com.crm.quartz;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.crm.entity.Askers;
import com.crm.entity.Netaskers;
import com.crm.entity.Student;
import com.crm.service.AskersService;
import com.crm.service.NetaskersService;
import com.crm.service.StudentService;

@Component
@EnableScheduling
public class QuartzAutoFenPei {
	@Autowired
	private NetaskersService  netaskersService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private AskersService askersService;
	
	//每隔5秒执行一次：*/5 * * * * ?
	@Scheduled(cron=" 0 0 7-23 * * ? ") //每天7点到23点，每整点执行一次
	public void DingShi() {
		System.out.println("我执行了定时任务(自动分配)");
		Auto();
	}
	
	
	
	
	public void Auto() {
		List<Netaskers> nlist = netaskersService.StartAuto();
		for(int v=0;v<nlist.size();v++) {
			//这样可以取一个整数出来DecimalFormat   df=new DecimalFormat("#");
			//这样是取小数点后一位
			DecimalFormat   df=new DecimalFormat("#.#");  
			//取double的整数部分
			DecimalFormat   sdf=new DecimalFormat("#"); 
			//学生总数
			int count=netaskersService.StuCount(nlist.get(v).getNetAskerId());
			//学生数据
			List<Student> slist = studentService.Cha(nlist.get(v).getNetAskerId());
			//有多少咨询师 and 咨询师的数据(已签到的咨询师)
			List<Askers> alist = askersService.GetQianDaoAll();
			//用来存放咨询师的权重
			Double[] xuan = new Double[alist.size()];
			for(int i =0; i<alist.size();i++) {
				xuan[i]=(double)alist.get(i).getWeight();
			}
			//接收权重总比例(份数)
			double x=0.0;
			for (int i = 0; i < xuan.length; i++) {
				x+=xuan[i];
			}
			//用来存放根据权重计算出来的比例
			Double[] ssr = new Double[xuan.length];
			for(int i=0;i<xuan.length;i++) {
				String g=String.valueOf(df.format((xuan[i]/x)*count));
				String [] ds=String.valueOf(g).split("\\.");
				Integer s=0;
				//判断比例和总数相乘是否正好是一个整数(如果是整数下面的判断将不会执行,会执行下一个for循环)
				if(ds.length!=1) {
					s=Integer.parseInt(ds[1]);
				}
				//判断小数是否进行四舍五入
				if(s>5) {
						ssr[i]=(xuan[i]/x)*count+1;				
					}else {
						ssr[i]=(xuan[i]/x)*count;			
					}	
			}
			List<Integer> st= new ArrayList<Integer>();
			//上面得到的数据都是double类型的，这里把它转化为整数类型 就是分配的学生数量
			Integer jie=0;
			for(int i=0;i<ssr.length;i++) {
				String g=String.valueOf(df.format(ssr[i]));
				String [] ds=String.valueOf(g).split("\\.");
				//Integer s=Integer.parseInt(ds[1]);
				jie=Integer.parseInt(ds[0]);
				st.add(jie);
				System.out.println(jie);
			}
			List<String> sidlist = new ArrayList<String>();
		
            sidlist.addAll(netaskersService.StuId(nlist.get(v).getNetAskerId()));
          //实现自动分配
			int c=0;
	for(int i =0;i<st.size();i++) {
	
		for(int j=0;j<st.get(i);j++) {
			c++;
		    System.out.println("我是I:"+i);
			System.out.println(c);
			if(i==0) {
				
				studentService.FenPeiStu(sidlist.get(j), alist.get(i).getAskerId());
				 
			} 
				if(i!=0) {
					
					studentService.FenPeiStu(sidlist.get(c-1), alist.get(i).getAskerId());	
				}

			}
	
	}		
		}
		
		
	}
	
	
}
